#include "UI.h"

//@author A0073611N
UI::UI()
{
	string input;
	string name;
	double budget,cashAvail;
	
	_curUser = User();
	if(_curUser.existUser())
		_curUser.loadUser();
	else						//new user
	{
		printHeader();
		cout<<"Hello. May I know what is your name?"<<endl; 
		getline(cin,input);
		istringstream in(input);
		in>>name;
		cout<<endl;
		cout<<"Hello. May I know how much is your monthly budget?"<<endl; 
		getline(cin,input);
		budget=atof(input.c_str());
		cout<<endl;
		cout<<"Hello. May I know how much cash is available on hand?"<<endl; 
		getline(cin,input);
		cashAvail=atof(input.c_str());

		_curUser = User(name,budget,cashAvail);
		_curUser.saveUser();
	}
	_curActionManager.initialize(&_curUser);
	_curPage.initialize(&_curUser, &_curActionManager);
	clear();
}

//@author A0073611N
void UI::printHeader()
{
	cout<<setfill('=')<<setw(135)<<"=\n\
				EzXpns\n\
	          Your Personalized Expense Manager\n";
	cout<<setfill('=')<<setw(78)<<"="<<endl;
}

//@author U095387X
void UI::printNotification(){
	string userName=_curUser.getName();
	double cashAvail=_curUser.getCashOnHand();
	double budget=_curUser.getBudget();
	double totalExpense = _curUser.getTotalExpense();
	time_t currTime;
	struct tm *now;
	time(&currTime);  /* get current time; same as: timeOfRecord = time(NULL)  */
	now = localtime(&currTime);

	list<Wish> wishList =_curUser.getUserAccount()->readWish();

	list<Wish>::iterator iter = wishList.begin();
	double wishBudget = 0;
	time_t inputTime, expectedTime;

	if(!wishList.empty()){
		while (iter != wishList.end()) {
			if(currTime <= iter->getExpectedDate()){
				struct tm * inputTimeInfo;
				struct tm * expectedTimeInfo;

				inputTime = (time_t) iter->getTimeOfInput();
				expectedTime = (time_t) iter->getExpectedDate();

				inputTimeInfo = localtime(&inputTime);
				int lastMonth = inputTimeInfo->tm_mon;
				int lastYear = inputTimeInfo->tm_year;
				expectedTimeInfo = localtime (&expectedTime);
				int thisMonth = expectedTimeInfo->tm_mon;
				int thisYear = expectedTimeInfo->tm_year;

				int monthPass;
				monthPass =	(thisYear - lastYear)*12 + (thisMonth - lastMonth);

				wishBudget += iter->getAmount()/monthPass;
			}
			iter++;

		}
	}
	
	cout<<"Welcome "<<userName<<". You still have $"<< floor(cashAvail * 100 + 0.5) / 100 <<" on hand. "<<endl;
	cout << "You have spent $" <<floor(totalExpense * 100 + 0.5) / 100<< " this month. "<< endl;
	cout << "You only have $" << floor((budget-totalExpense) * 100 + 0.5) / 100 <<" left for the montly budget."<<endl;

	if(wishBudget) cout << "To purchase your wish items, you need to save $" << floor(wishBudget * 100 + 0.5) / 100 << " this month." << endl;
	cout << "Current local time and date: " << asctime(now) << endl;
}

//@athor U095387X
void UI::printAction(){
	if(_curActionManager.getNumberOfLastActions())
		cout << "Last Action: " << _curActionManager.LastActionToString() << endl << endl;
}

//@author U095387X
Menu& UI::getMenu(){
	return _curMenu;
}

//@author U095387X
Page& UI::getPage(){
	return _curPage;
}

//@author U095387X
void UI::printMenu(){
	cout << _curMenu.menuString() << endl;
	return;
}

//@author U095387X
void UI::getMenuInput(int menuInput){
	string input;

	if(menuInput == 0){
		getline(cin, input);
		if(input == "x")
			menuInput = -1;
		else
			menuInput = atoi(input.c_str());
	}

	_curMenu.inputMenu(menuInput);
}

//@author U095387X
void UI::clear(){
	system("CLS");
}

